﻿layui.use(['form'], function() {
	var form = layui.form;
	form.render();

	function login(account, passWord) {
		$.ajax({
			type: "POST", //发送请求的类型
			url: "http://localhost:8080/ssmService/index/login", //请求后台的接口路径
			async: true, //异步请求
			data: {
				"user_account": account,
				"user_password": passWord
			}, //设置参数
			//ajax执行之前的回调函数
			"beforeSend": function() {
				layer.load(1, {
					shade: [0.5, '#fff'] //0.1透明度的白色背景
				});
			},
			//ajax执行成功后的回调函数
			"success": function(result) {
				//result为ajax所响应的数据
				console.log(result);
				if(result.resultCode == 0) {
					localStorage.setItem("userId", result.data.user_id);
					localStorage.setItem("userName", result.data.name);
					localStorage.setItem("headImg", result.data.head_img_url);
					location.href = "views/index.html";
				} else {
					layer.msg("登陆失败", {
						icon: 2, //图标
						time: 1000, //时间
						top: 300,
						left: 500
					});
				}
			},
			"error": function() {
				layer.msg("未知错误，请重新刷新页面", {
					icon: 2, //图标
					time: 1000, //时间
					top: 300,
					left: 500
				});

				/*layer.alert("服务器奔溃啦，请重新启动页面",{
					icon:2,
					btn:["取消","确定"]
					
				});*/
				/*layer.confirm("服务器奔溃啦，请重新启动页面",{
					icon: 2, //图标
					top:300,
					left: 500
				});*/
			}

		});

	}
	form.on('submit(login)', function() {
		var account = $("#account").val();
		var passWord = $("#passWord").val();
		console.log(account);
		console.log(passWord);
		//加密字符串
	    var rsa_passWord=rsaSecurity(passWord);
	     console.log(rsa_passWord);
		login(account, rsa_passWord);
		if($("#remember").is(":checked")) {
			localStorage.setItem("account", account);
			localStorage.setItem("passWord", passWord)
		} else {
			localStorage.setItem("account", "");
			localStorage.setItem("passWord", "")
		}
		return false;
	});

	function remembers() {
		if(localStorage.getItem("account") != "" && localStorage.getItem("passWord") != "") {
			$("#account").val(localStorage.getItem("account"));
			$("#passWord").val(localStorage.getItem("passWord"));
			$("#remember").prop("checked", true);

		}
		form.render();
	}

	remembers();

	/**
	 * 添加验证码
	 */
	function addUnlock() {
		//定义一个英文字母的数组
		var engLish = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
		//定义一个字母下标数组
		var attr = [];

		//定义最终的验证码的值
		var code = "";
		//通过循环获取不重复值得字母下标数组
		for(var i = 0; i < 4; i++) {
			var ranNum = Math.ceil(Math.random() * 25);
			if(attr.indexOf(ranNum) == -1) {
				attr.push(ranNum);
			} else {
				i--;
			}
		}
		//通过下标数组将字母数组的字母取出并赋值给code
		for(var i = 0; i < 4; i++) {
			code += engLish[attr[i]];
			console.log(engLish[attr[i]]);
		}

		$(".captcha_gbws_wrap").remove();
		$(".login").buttonCaptcha({
			codeWord: code, //匹配的验证码
			codeZone: code, //验证码正确摆法的提示
			captchaHeader: "请正确的将字符移动到框内", //提示信息
			captchaUnlocked: "通过验证" //通过验证
		});
		$(".captcha_gbws_wrap").appendTo("#verifyGroup");
	}
	
	//rsa加密
	function rsaSecurity(password){
		console.log(password);
		var rsa_str;
		$.ajax({
			type:"get",
			url:"http://localhost:8080/ssmService/rsa/generateRSAJsKey",
			async:false,
			"success":function(result){
				//从服务端获取加密模板
				var modulus=result.split(";")[0];
				//获取公钥指数
				var public_exponent=result.split(";")[1];
				//公钥指数，加密模板
				var public_key= new RSAUtils.getKeyPair(public_exponent,"",modulus);
				//对登录密码进行颠倒
				var rsa_pass=password.split("").reverse().join("");
				//获取到加密字符串 参数（公钥，颠倒后的密码）
				rsa_str=RSAUtils.encryptedString(public_key, rsa_pass);
			}
		});
		
		return   rsa_str;
	}
	
//	function rsaEncryption  (str) {
//      var encrypedValue = str;
//      $.ajax({
//          url: requestUtilParams.action.rsa,
//          async: false,
//          success: function (res) {
//              //加密模
//              var Modulus = res.split(';')[0];
//              //公钥指数
//              var public_exponent = res.split(';')[1];
//              //通过模和公钥参数获取公钥
//              var key = new RSAUtils.getKeyPair(public_exponent, "", Modulus);
//              //颠倒密码的顺序，要不然后解密后会发现密码顺序是反的
//              var reversedPwd = str.split("").reverse().join("");
//              //对密码进行加密传输
//              var encrypedPwd = RSAUtils.encryptedString(key, reversedPwd);
//              encrypedValue = encrypedPwd;
//          }
//      })
//
//      return encrypedValue;
//  }
	//调用生成验证码的方法
	addUnlock();

});